﻿using CSRedis;
using God.ShoppingWebApi.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Threading.Tasks;
using God.ShoppingWebApi.DTO.Shopping;

namespace God.ShoppingWebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShoppingCartController : ControllerBase
    {
        public ShoppingCartController()
        {
            RedisHelper.Initialization(_client);
        }
        private CSRedisClient _client = new CSRedisClient("127.0.0.1:6379,password=123456");
        [HttpPost]
        public IActionResult AddCart(ShoppingCartDto shoppingCartDto)
        {
            try
            {
                //先获取redis里有没有购物车列表
                List<ShoppingCartDto> userCar = RedisHelper.Get<List<ShoppingCartDto>>($"LiveShoppingCar:{shoppingCartDto.UserId}");
                if (userCar==null)
                {
                    List<ShoppingCartDto> newcar = new List<ShoppingCartDto>()
                    {
                        shoppingCartDto
                    };
                    //保存到redis
                    bool result = RedisHelper.Set($"LiveShoppingCar:{shoppingCartDto.UserId}", newcar);
                    
                }
                else
                {
                    //查询是不是同一款商品，如果是则叠加上新的数量
                    var item = userCar.Where(x => x.CommodityId == shoppingCartDto.CommodityId).FirstOrDefault();
                    if(item!=null)
                    {
                        item.ShoppingCartNum += shoppingCartDto.ShoppingCartNum;

                    }
                    else
                    {
                        userCar.Add(shoppingCartDto);
                    }
                    //保存到redis
                    bool result = RedisHelper.Set($"LiveShoppingCar:{shoppingCartDto.UserId}", userCar);
                }
            }
            catch (Exception)
            {

                return Ok(new { reust = "添加购物车成功", code = 1 });
            }
            return Ok(new { reust = "添加购物车失败", code = -1 });
        }
        /// <summary>
        /// 购物车显示
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult CarShow(string UserId)
        {
           
            var  list= RedisHelper.Get<List<ShoppingCartDto>>($"LiveShoppingCar:{UserId}");
            
          
            return Ok(list);
        }
        /// <summary>
        /// 购物车删除
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Del(string UserId)
        {
          long i=  RedisHelper.Del($"LiveShoppingCar:{UserId}");
            if (i>0)
            {
                return Ok(new { result = "删除成功", code = 1 });
            }
            else
            {
                return Ok(new { result = "删除失败", code = -1 });
            }
        }
        /// <summary>
        /// 购物车修改
        /// </summary>
        /// <param name="shoppingCartDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upt(ShoppingCartDto dto)
        {
            var line = RedisHelper.Get<List<ShoppingCartDto>>($"LiveShoppingCar:{dto.UserId}");
            var item = line.Where(x => x.CommodityId == dto.CommodityId).FirstOrDefault();
            if (item!=null)
            {
                item.ShoppingCartNum += dto.ShoppingCartNum;
            }
            else
            {
                return BadRequest();//400错误信息
            }
            //保存到redis
            bool result = RedisHelper.Set($"LiveShoppingCar:{dto.UserId}", item);

            return Ok(new { result="修改成功",code=1});
        }
    }
}
